<?php
/**
 * Created by PhpStorm.
 * User: helloxingtongshi
 * Date: 2016-05-20
 * Time: 15:42
// */
//isset方法用来检测方法是否存在
if(isset($_POST["updateclient"]) || isset($_POST["addclient"])){
    //接收数据
    $client = array(
        'Id' => empty($_POST['Id']) ? 0 : $_POST['Id'],
        'producename' =>@$_POST['producename'],
        'name' => $_POST['name'],
        'phone' => $_POST['phone'],
        'bankid' => $_POST['bankid'],
        'bankname' => $_POST['bankname'],
        'adress' => $_POST['adress'],
        'num' => $_POST['num'],
        'buytime' => $_POST['buytime'],
        'sum' => $_POST['sum'],
        'price' =>@$_POST['price'],
    );
}
if(!isset($_POST["addclient"])&&!isset($_POST["updateclient"])&&isset($_POST["producename"])){
    //接收选择商品名称后的数据
    $addClients = array(
        'Id' => empty($_POST['Id']) ? 0 : $_POST['Id'],
        'producename' => $_POST['producename'],
        'name' => $_POST['name'],
        'phone' => $_POST['phone'],
        'bankid' => $_POST['bankid'],
        'bankname' => $_POST['bankname'],
        'adress' => $_POST['adress'],
        'num' => $_POST['num'],
        'buytime' => $_POST['buytime'],
        'sum' => $_POST['sum'],
        'price' => @$_POST['price'],
    );
}
//使用require语句包含并执行sql.php
require 'sql.php';
//从produce表中遍历id与name,price，数据存储于二维数组$cproduce1。将其与post来的其他值一起传到数据库client表
$select1 = "select id,name,price from produce";
$aresult1 = mysql_query($select1);
while ($row = mysql_fetch_row($aresult1)) {
    $cproduce1[] = $row;
}

//添加
if(isset($_POST["addclient"])){
    $sql="insert into client(producename,name,phone,bankid,bankname,adress,num,buytime,sum,price)values('{$client['producename']}','{$client['name']}','{$client['phone']}','{$client['bankid']}','{$client['bankname']}','{$client['adress']}','{$client['num']}','{$client['buytime']}','{$client['sum']}','{$client['price']}')";
    $result = mysql_query($sql);
    if($result > 0){
        echo "添加成功";
    }else{
        echo "添加失败";
    }
}
//删除
if(isset($_POST["delclient"])){
    $id = $_POST['id'];
    $del="delete from client where id=$id";
    $resultd= mysql_query($del);
    if($resultd != 0){
        echo  "删除成功";
    }else{
        echo  "删除失败".mysql_error();
    }
}
//修改
if(isset($_POST["updateclient"])){
    $id = $_POST['id'];
    $update="update client set  name='{$client['name']}',num='{$client['num']}',sum='{$client['sum']}',buytime='{$client['buytime']}',phone='{$client['phone']}',adress='{$client['adress']}',bankname='{$client['bankname']}',bankid='{$client['bankid']}' where id=$id";
    $resultu= mysql_query($update);
    if($resultu != 0){
        echo  "更新成功";
    }else{
        echo  "更新失败".mysql_error();
    }
}

//从client表中查询遍历全部数据，再传送到页面
$add="select name,producename,num,sum,buytime,phone,adress,bankname,bankid,id,price from client";
$aresult= mysql_query($add);
while($row=mysql_fetch_row($aresult)){
    $clientl[]=$row;
}
?>